package com.qf.controller;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/17 16:12
 */

import com.qf.service.GoodsProductService;
import com.qf.service.GoodsService;
import com.qf.service.OrderTotalService;
import com.qf.service.UserTotalService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 首页
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/dashboard")
public class IndexContorller {
    @Autowired //查询用户数量
    private UserTotalService userTotalService;

    @Autowired  //订单商品
    private OrderTotalService orderTotalService;

    @Resource(name = "goods")
    private GoodsService goodsService;

    @Autowired
    private GoodsProductService goodsProductService;

    @RequestMapping("")
    public Object Count(){
        int userTotalcount = userTotalService.UserTotalcount();
        int oderTotalCount = orderTotalService.OderTotalCount();
        int goodsProductCount = goodsProductService.GoodsProductCount();
        int goods = goodsService.countGoods();
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsTotal",goods);
        map.put("userTotal",userTotalcount);
        map.put("productTotal",goodsProductCount);
        map.put("orderTotal",oderTotalCount);
        return ResponseUtil.ok(map);
    }
    @RequestMapping("/chart")
    public Object dashboard(){
        //查询每日用户增长的数量
        List<DayStatis> dayUser = userTotalService.incrCount();
        System.out.println("查询每日用户增长的数量"+dayUser);
        //查询每日订单增长数量
        List<DayStatis> dayOrder = orderTotalService.DayOderCount();
        System.out.println("查询每日订单增长数量"+dayOrder);
        //日期和对应的订单总额, 订单笔数
        List<DayStatis> dayStatis = orderTotalService.orderNumber();
        System.out.println("日期和对应的订单总额, 订单笔数"+dayStatis);

        //商品分类和对应分类的销售额
        List<CategorySellAmts> category = orderTotalService.categoryCount();
        System.out.println("商品分类和对应分类的销售额"+category);
        //调用方法
        CategorySellVo categoryname = categoryname(category);
        OrderAmtsVo orderAmtsVo = DateAndCorreSpon(dayStatis);

        UserOrderCntVo orderCntVo = new UserOrderCntVo();
        String[] strings = unionDayData(dayUser, dayOrder);
        System.out.println("日期数组"+strings);
        //日期数组
        orderCntVo.setDayData(strings);
        //用户数量和对应的日期
        int[] ints = fetchArrayCnt(strings,dayUser);
        orderCntVo.setUserCnt(ints);
        //用户订单和对应的日期
        int[] cnt = fetchArrayCnt(strings, dayOrder);
        orderCntVo.setOrderCnt(cnt);
        HashMap<String, Object> map = new HashMap<>();
        //添加: 商品分类和对应分类的销售额
        map.put("categorySell",categoryname);
        map.put("orderAmts",orderAmtsVo);
        map.put("userOrderCnt",orderCntVo);
        return  ResponseUtil.ok(map);
    }

    private int[] fetchArrayCnt(String[] dayData, List<DayStatis> dayStatis){
        int[] ints = new int[dayData.length];

        for(int i=0;i<dayData.length;i++){
            int count=0;
            String dayDatum = dayData[i];
            for (DayStatis stati : dayStatis) {
                if(dayDatum.equals(stati.getDayStr())){
                    count=stati.getCnts();
                    break;
                }
            }
            ints[i]=count;
        }
        return ints;
    }

    // 商品分类和对应分类的销售额
    private CategorySellVo categoryname(List<CategorySellAmts> categorySellAmts ){
        CategorySellVo sellVo = new CategorySellVo();
        String[] strings = new String[categorySellAmts.size()];

        if(categorySellAmts!=null ||strings.length==0){
            for (int i=0;i<categorySellAmts.size();i++) {
                strings[i]= categorySellAmts.get(i).getName();
            }
        }
        sellVo.setCategoryNames(strings);
        sellVo.setCategorySellData(categorySellAmts);
        return sellVo;
    }
//    日期和对应的订单总额, 订单笔数
    private  OrderAmtsVo DateAndCorreSpon(List<DayStatis> dayStatis){
        OrderAmtsVo vo = new OrderAmtsVo();
        String[] dayData = new String[dayStatis.size()];
        int[]  orderCntData = new int[dayStatis.size()];
        BigDecimal[] orderAmtData = new BigDecimal[dayStatis.size()];

        if(dayStatis.size()!=0||dayStatis!=null){
            for (int i=0;i<dayStatis.size(); i++) {
                dayData[i]= dayStatis.get(i).getDayStr();
                orderCntData[i]=dayStatis.get(i).getCnts();
                orderAmtData[i]=dayStatis.get(i).getAmts();
            }
        }
        vo.setDayData(dayData);
        vo.setOrderAmtData(orderAmtData);
        vo.setOrderCntData(orderCntData);
        return vo;
    }
    //集合日期
    private String[]  unionDayData( List<DayStatis> dayUser,List<DayStatis> dayOrder ){
        Set<String> set = new HashSet<>();
        for (DayStatis statis : dayUser) {
            set.add(statis.getDayStr());

        }
        for (DayStatis statis : dayOrder) {
            set.add(statis.getDayStr());
        }
        List<String> list = new ArrayList<>(set );
        Collections.sort(list);
        return list.toArray(new String[0]);
    }


}
